import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import service from '@dfeidao/atom-web/msg/service';
import error from '@dfeidao/atom-web/msgbox/error';
import success from '@dfeidao/atom-web/msgbox/success';
import direct_to from '@dfeidao/atom-web/url/direct-to';
import { productid } from '../../atom/config';
export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// todo
	const user_no = get(fd, 'user_no');
	// 原密码
	const captcha = fd.data.node.querySelector<HTMLInputElement>('[ymm]').value;
	// 新密码
	const xmm = fd.data.node.querySelector<HTMLInputElement>('[xmm]').value;
	// 确定密码
	const password = fd.data.node.querySelector<HTMLInputElement>('[qrmm]').value;
	// const productid = get(fd, 'productid');
	// const user_code = user.userinfo.usercode;
	const user_pwd_regx = /^[0-9]{6,}$/;
	if (captcha === '' || xmm === '' || password === '') {
		return error('请填写完整信息');
	}
	if (!user_pwd_regx.test(xmm) || '') {
		return error('您输入的密码不符合要求！');
	}

	if (xmm !== password) {
		error('确认密码错误');
		return;
	}
	if (captcha === xmm) {
		error('新旧密码不能相同');
		return;
	}


	// loginservice.updatepwd 修改密码服务
	const msg = {
		captcha,// 旧密码
		edit_type: 'normal', // 修改密码方式
		id: user_no,// 可以用用户名代替
		password,// 新密码
		productid
	};
	const s = await service('loginservice.updatepwd', msg) as { result: boolean; msg: string; };
	if (s.msg === '原始密码不正确') {
		error('旧密码不对');
		return;
	}
	if (s.result === true) {
		success('修改成功');
		direct_to('./login.html');
		return;
	} else {
		error('修改失败');
		return;
	}
}
